package com.example.demo.controller;

import com.example.demo.entity.Product;
import com.example.demo.entity.VideoSource;
import com.example.demo.service.ProductServiceImpl;
import com.example.demo.service.VideoSourceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/18 0018.
 */
@Controller
@RequestMapping("/index")
public class IndexController {
@Autowired
private VideoSourceServiceImpl videoSourceService;
    @Autowired
    private ProductServiceImpl productService;

    //增加商品信息
    @RequestMapping(value = "/addProduct")
    public String addProduct(){
        return "product";
    }

    //删除商品信息
    @RequestMapping(value = "/delProduct")
    public String delProduct(){
        return "product";
    }

    //修改商品信息
    @RequestMapping(value = "/updateProduct")
    public String modifyProduct(@RequestBody Product product){
        System.out.println("IndexController p.getId()==="+product.getId());
        Product pro=productService.findById(product.getId());
        pro.setName(product.getName());
        pro.setPrice(product.getPrice());
        pro.setStanderd(product.getStanderd());
        pro.setCode(product.getCode());
        pro.setSrcPath(product.getSrcPath());
        productService.save(pro);
        return "redirect:/index/findProduct";
    }
    //查询商品信息
    @RequestMapping(value = "/findProduct")
    public String findProduct(Map map){
        List<Product> productList=productService.findAll();
        map.put("productList",productList);
        return "product";
    }

    //查询商品信息,返回json
    @RequestMapping(value = "/findProductJson")
    @ResponseBody
    public List<Product> findProductJson(Map map){
        List<Product> productList=productService.findAll();
        map.put("productList",productList);
        return productList;
    }
    //新增商品信息,重定向到查询所有商品,返回json
    @RequestMapping(value = "/saveProduct",method = RequestMethod.POST)
    public String  saveProduct(Product p){
            productService.save(p);
            return "redirect:/index/findProductJson";
    }
    //删除商品信息,重定向到查询所有商品,返回json
    @RequestMapping(value = "/delProduct",method = RequestMethod.POST)
    public String  delProduct(int id){

            productService.del((long)id);
            return "redirect:/index/findProduct";
    }

    @GetMapping("/manager")
    public String manager(){
        return "manager";
    }

    @RequestMapping("/index")
    public String index(Map map){
        map.put("name","test");
        return "index";//return 返回的是模板名称xx.html
    }

    @GetMapping("/formBuilder")
    public String builderForm(){
        return "formBuilder";
    }

    @GetMapping("/getAllVideo")
    public List<VideoSource> all(){
        List<VideoSource> videoSourceList=videoSourceService.getAllVideoInfo();
        System.out.print("----------"+videoSourceList.toString());
        return videoSourceList;
    }
}
